package org.zoomdev.zoom.server.modules;

import org.zoomdev.zoom.aop.annotations.NoEnhance;
import org.zoomdev.zoom.common.config.ConfigReader;
import org.zoomdev.zoom.ioc.Ioc;
import org.zoomdev.zoom.ioc.IocTypeKey;
import org.zoomdev.zoom.ioc.annotations.Inject;
import org.zoomdev.zoom.ioc.annotations.IocBean;
import org.zoomdev.zoom.ioc.annotations.Module;
import org.zoomdev.zoom.ioc.annotations.ModuleInit;
import org.zoomdev.zoom.server.WebAppServer;
import org.zoomdev.zoom.web.WebService;


@NoEnhance
@Module
public class SystemWebServerModule {


    @Inject(config = "zoom.server.port")
    private int port = WebAppServer.DEFAULT_PORT;
    @Inject(config = "zoom.server.contextPath")
    private String contextPath = WebAppServer.DEFAULT_CONTEXT_PATH;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    /**
     * 解析参数
     *
     * @return
     */
    @IocBean
    public WebAppServer getWebAppServer(InnerFilter innerFilter) {
        return new WebAppServer(port, contextPath, innerFilter);
    }

    @ModuleInit(after = true)
    public void init(Ioc ioc) throws Exception {
        WebService webService = ioc.get(new IocTypeKey(WebService.class));
        if (webService != null) {
            return;
        }

        WebAppServer server = ioc.fetch(WebAppServer.class);
        ConfigReader reader = ioc.fetch(ConfigReader.class);

        String disable = reader.getString("zoom.server.disable");
        if ("true".equals(disable)) {
            return;
        }
        server.startup();
    }
}
